package com.boxin.ims.modules.bi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.search.indexes.serialization.javaserialization.impl.Add;

import com.alibaba.fastjson.JSON;
import com.boxin.ims.modules.bi.entity.Series;

public class DateProcessingUtils {
	public static List<Map<String,Object>> dayFillUp(List<Map<String,Object>> list){
		
		return list;
	}
	
	
	/**
	 * 考虑当天没有记录，补0操作
	 *@author xiaoqian
	 * @param month
	 * @param year
	 * @return
	 * 2014-3-24上午9:17:44
	 */
	public static String[] daysMountBymonth(String month,String year){
		if(month.equals("01")||month.equals("03")||month.equals("05")||month.equals("07")||month.equals("08")||month.equals("10")||month.equals("12")){
			String[] Strs=new String[31];
			for (int i = 0; i < Strs.length; i++) {
				Strs[i]=i+1+"";
			}
			return Strs;
		}else if(month.equals("04")||month.equals("06")||month.equals("09")||month.equals("11")){
			String[] Strs=new String[30];
			for (int i = 0; i < Strs.length; i++) {
				Strs[i]=i+1+"";
			}
			return Strs;
		}else{
			if((Integer.parseInt(year)%4==0&&Integer.parseInt(year)%100!=0)||(Integer.parseInt(year)%400==0)){
				String[] Strs=new String[29];
				for (int i = 0; i < Strs.length; i++) {
					Strs[i]=i+1+"";
				}
				return Strs;
			}else{
				String[] Strs=new String[28];
				for (int i = 0; i < Strs.length; i++) {
					Strs[i]=i+1+"";
				}
				return Strs;
			}
		} 
		
	}
	
	
	
	public static Integer[] getData(String[] ints,List<Map<String,Object>> list){
		Integer[] data=new Integer[ints.length];
		
		for (int j = 0; j < ints.length;j++) {
			Boolean flag=false;
			for (int i = 0; i < list.size(); i++) {
				String day=list.get(i).get("day").toString();
				if(ints[j].equals(processingDay(day))){
					flag=true;
					data[j]=Math.abs(Integer.parseInt(list.get(i).get("count").toString())) ;
				}
			}
			if(flag==false){
				data[j]=0;
			}
		}
		return data;
	}
	
	public static List<Map<String, Object>>  getDataAndLink(String[] ints,List<Map<String,Object>> list,String time){
		
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		
		for (int j = 0; j < ints.length;j++) {
			Boolean flag=false;
			Map<String, Object> map =new HashMap<String, Object>();
			for (int i = 0; i < list.size(); i++) {
				String day=list.get(i).get("day").toString();
				if(ints[j].equals(processingDay(day))){
					flag=true;
					map.put("y", Math.abs(Integer.parseInt(list.get(i).get("count").toString())));
					map.put("url", "/zxims/a/bi/aliveMember?day="+getDay(time,Integer.parseInt(ints[j])));
					data.add(map);
				}
			}
			if(flag==false){
				map.put("y", 0);
				map.put("url", "");
				data.add(map);
			}
		}
		return data;
	}
	
	public static Integer[] getDataByWeek(List<Map<String,Object>> list,Date day) throws NumberFormatException, ParseException{
		Integer[] data=new Integer[7];
		 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		 Calendar cal = Calendar.getInstance();
		 
		 
		
		for (int j = 0; j < data.length;j++) {
			Boolean flag=false;
			int amount=j-3;
			cal.setTime(day);
			cal.add(Calendar.DAY_OF_YEAR, amount);
			for (int i = 0; i < list.size(); i++) {
				String day1=list.get(i).get("day").toString();
				System.out.println(format.format(cal.getTime())+"-----"+day1);
				System.out.println(format.format(cal.getTime()).equals(day1));
				if(format.format(cal.getTime()).equals(day1)){
					flag=true;
					data[j]=Math.abs(Integer.parseInt(list.get(i).get("count").toString())) ;
				}
			}
			if(flag==false){
				data[j]=0;
			}
		}
		return data;
	}
	
	public static String processingDay(String day){
		day=  day.substring(day.length()-2, day.length());
		if(day.valueOf(0).equals("0")){
			if(day.charAt(0)=='0'){
				day=day.replace("0", "");
			}
		}
		return day;
	}
	
	public static String getDay(String time,Integer i){
		if(i<10){
			return time+"0"+i;
		}else{
			return time+i;
		}
	}
	
	
	public static String getmonth(){
		Calendar cal = Calendar.getInstance();
		int month=cal.get(Calendar.MONTH) + 1;
		if(month<10){
			return "0"+month;
		}else{
			return ""+month;
		}
	}
	
	
	
	public static String getSceneId(String message){
		String str=message;
		int index=str.indexOf("qrscene_");
		int last=str.lastIndexOf("Ticket：");
		if(last==-1||index==-1){
			return "-1";
		}
		System.out.println(str.substring((index+8), last));
		return str.substring((index+8), last);
	}
	
	
	public static StringBuffer getSeriesJson(Series series){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("{");
		stringBuffer.append("data:").append(JSON.toJSONString(series.getData())).append(",");
		stringBuffer.append("name:").append("\"").append(series.getName()).append("\"").append(",");
		if(series.getTooltip()!=null&&series.getTooltip().getValueSuffix()!=null){
			stringBuffer.append("tooltip:{");
			stringBuffer.append(" valueSuffix:").append("\"").append(series.getTooltip().getValueSuffix()).append("\"");
			stringBuffer.append(" }, ");
		}
		stringBuffer.append("type: ").append("\"").append(series.getType()).append("\"").append(",");
		stringBuffer.append("pointInterval: ").append(series.getPointInterval()).append(",");
		stringBuffer.append("pointStart: ").append(series.getPointStart());
        stringBuffer.append("}");
        return stringBuffer;
		
	}
	
	
	
	public static Map<String, Object> processingRechargeLog(List<Map<String, Object>>  list){
		
		Integer recharge=0;
		Integer expense=0;
		Integer exception=0;
		Map<String , Object> map = new HashMap<String , Object>();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i)!=null&&list.get(i).get("type")!=null&&list.get(i).get("type").toString().equals("0")){
				recharge+=Integer.parseInt(list.get(i).get("money").toString());
			}
			if(list.get(i)!=null&&list.get(i).get("type")!=null&&list.get(i).get("type").toString().equals("1")){
				expense+=Integer.parseInt(list.get(i).get("money").toString());
			}
			if(list.get(i)!=null&&list.get(i).get("type")!=null&&list.get(i).get("type").toString().equals("2")){
				exception+=Integer.parseInt(list.get(i).get("money").toString());
			}
		}
		map.put("recharge",Math.abs(recharge));
		map.put("expense", Math.abs(expense));
		map.put("exception", Math.abs(exception));
		return map;
	}
	
	
	
}
